package com.paishen.peiniwan.frame.push;

import android.app.NotificationManager;
import android.content.Context;

import com.paishen.common.util.LogUtil;
import com.paishen.common.util.SpfsUtil;
import com.paishen.peiniwan.BusEvent;
import com.paishen.peiniwan.C;

import de.greenrobot.event.EventBus;

/**
 * Created by peng on 2016/01/07.
 */
public class PushController {

    static PushController instance;
    private Context appContext;
    private NotificationManager notificationManager;

    public synchronized static PushController getInstance() {
        if (instance == null) {
            instance = new PushController();
        }
        return instance;
    }

    public void init(Context context) {
        appContext = context;
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public void dealNotification(int type, String content) {
        try {
            switch (type) {
                case C.push.TYPE_DATE_RECOMMENDED:
                    LogUtil.d("TYPE_DATE_RECOMMENDED ACTION_REFRESH_MAIN_DATE_NEW_DATE");
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_REFRESH_MAIN_DATE_NEW_DATE));
                    break;
                case C.push.TYPE_DATE_BE_RESPONDED:
                    LogUtil.d("TYPE_DATE_BE_RESPONDED ACTION_REFRESH_DATE_CHOOSE");
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_REFRESH_DATE_CHOOSE));
                    break;
                case C.push.TYPE_DATE_REFRESH_ORDER_INFO:
                    LogUtil.d("TYPE_DATE_REFRESH_ORDER_INFO ACTION_REFRESH_MAIN_PROFILE_DATE");
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_REFRESH_MAIN_PROFILE_DATE));
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_REFRESH_DATE_INFO_STATUS_BE_SELECTED));
                    break;
                case C.push.TYPE_DATE_BE_CANCELLED:
                    LogUtil.d("TYPE_DATE_BE_CANCELLED ACTION_REFRESH_MAIN_PROFILE_DATE");
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_REFRESH_MAIN_PROFILE_DATE));
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_REFRESH_DATE_INFO_STATUS_BE_CANCELLED));
                    break;
                case C.push.TYPE_DATE_PAY_COMPLETED:
                    LogUtil.d("TYPE_DATE_PAY_COMPLETED ACTION_REFRESH_MAIN_PROFILE_DATE");
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_REFRESH_MAIN_PROFILE_DATE));
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_REFRESH_DATE_INFO_STATUS_PAY_SUCCESS));
                    break;
                case C.push.TYPE_DATE_COMPLETED:
                    LogUtil.d("TYPE_DATE_PAY_COMPLETED ACTION_REFRESH_MAIN_PROFILE_DATE");
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_REFRESH_MAIN_PROFILE_DATE));
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_REFRESH_DATE_INFO_STATUS_BE_COMPLETE));
                    break;
                case C.push.TYPE_PROFILE_AUTH_REFRESH:
                    LogUtil.d("TYPE_PROFILE_AUTH_REFRESH TYPE_PROFILE_AUTH_REFRESH");
                    // SpfsUtil.write(C.sp.PUSH_PROFILE_NEW_AUTH, true);
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_REFRESH_MAIN_PROFILE_AUTH));
                    break;
            }
        } catch (Exception e) {
            LogUtil.e("PushController dealNotification fail type: " + type + "\n" + e.getMessage());
        }
    }

//    public void dealMessage(int type, String message) {
//        try {
//        } catch (Exception e) {
//            e.printStackTrace();
//            LogUtil.e("PushController dealMessage fail type: " + type );
//        }
//    }
//
//    private void sendNotification(int foregroundNotifyID, String contentTitle, String summaryBody) {
//        // create and send notificaiton
//        if (!isAppRunningForeground()) {
//            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(appContext)
//                    .setSmallIcon(appContext.getApplicationInfo().icon)
//                    .setWhen(System.currentTimeMillis())
//                    .setAutoCancel(true);
//
//            String notifyText = contentTitle;
//
//            Intent msgIntent = appContext.getPackageManager().getLaunchIntentForPackage(appContext.getApplicationInfo().packageName);
//            ;
//            PendingIntent pendingIntent = PendingIntent.getActivity(appContext, foregroundNotifyID, msgIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//
//            mBuilder.setContentTitle(contentTitle);
//            mBuilder.setTicker(notifyText);
//            mBuilder.setContentText(summaryBody);
//            if (pendingIntent != null) mBuilder.setContentIntent(pendingIntent);
//            // mBuilder.setNumber(notificationNum);
//            mBuilder.setDefaults(Notification.DEFAULT_SOUND);
//            Notification notification = mBuilder.build();
//
//            notificationManager.notify(foregroundNotifyID, notification);
//        }
//    }
//
//    private boolean isAppRunningForeground() {
//        ActivityManager var1 = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
//        List var2 = var1.getRunningTasks(1);
//        return appContext.getPackageName().equalsIgnoreCase(((ActivityManager.RunningTaskInfo) var2.get(0)).baseActivity.getPackageName());
//    }
}
